package com.hzw.saas.web.app.user.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.hzw.saas.api.user.ICustomerService;
import com.hzw.saas.api.user.IUserAccountService;
import com.hzw.saas.api.user.IUserProfileService;
import com.hzw.saas.api.user.IUserTripartiteService;
import com.hzw.saas.api.user.bo.CustomerBO;
import com.hzw.saas.api.user.bo.UserAccountBO;
import com.hzw.saas.api.user.bo.UserProfileBO;
import com.hzw.saas.api.user.bo.UserWeTripartiteBO;
import com.hzw.saas.api.user.query.CustomerGetQuery;
import com.hzw.saas.common.config.annotation.SysLog;
import com.hzw.saas.common.config.auth.param.PlatformParam;
import com.hzw.saas.common.config.util.AssertUtil;
import com.hzw.saas.common.security.pojo.dto.SaasUser;
import com.hzw.saas.common.security.utils.SecurityUtils;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.web.app.user.dto.UserSimpleInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author zzl
 * @since 04/08/2021
 */
@Validated
@Controller
@RequestMapping("/user")
@RequiredArgsConstructor
@Api(tags = "用户中心/基本信息")
public class UserCustomerController {

    @Resource(name = "customerServiceImpl")
    private ICustomerService customerService;
    private final IUserAccountService userAccountService;
    private final IUserProfileService userProfileService;
    private final IUserTripartiteService userWeTripartiteService;
    private final IUserTripartiteService userWeChatService;

    @SysLog
    @GetMapping("/info")
    @ApiOperation(value = "用户基本信息", notes = "用户基本信息：用户名、手机、邮箱、真实姓名")
    @ApiOperationSort(0)
    public ResponseEntity<UserSimpleInfo> getUserInfo() {
        String userId = SecurityUtils.getUser().getUserId();
        UserAccountBO account = userAccountService.getAccount(userId);
        UserProfileBO userProfileBO = userProfileService.get(userId);
        if(Objects.isNull(account) || Objects.isNull(userProfileBO)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
        }
        UserSimpleInfo userSimpleInfo = MapperUtil.nf().map(account, UserSimpleInfo.class);
        userSimpleInfo.setRealName(userProfileBO.getRealName());
        //查询用户三方接入信息
        List<PlatformParam> byUserId = userWeTripartiteService.getByUserId(userId);
        if (CollectionUtil.isNotEmpty(byUserId)){
            byUserId.forEach(platformParam -> {
                if (platformParam.getPlatform().equals("wechat")){
                    userSimpleInfo.setWechat(platformParam.getNickname());
                }
            });
        }
        return ResponseEntity.ok(userSimpleInfo);
    }

    @GetMapping("/customer")
    @SysLog
    @ApiOperation(value = "客户基本信息", notes = "获取客户的基本信息、会员级别、相关销售等")
    @ApiOperationSort(1)
    public ResponseEntity<CustomerBO> getCustomerIno() {
        SaasUser user = SecurityUtils.getUser();
        CustomerBO customer = customerService.getCustomer(new CustomerGetQuery().setUserName(user.getUsername()));
        return ResponseEntity.ok(customer);
    }

    @GetMapping("/wechat/check/{type}")
    @ApiOperation("判断当前登陆账户是否绑定")
    public ResponseEntity<Boolean> ifBound(@ApiParam("应用类型,前两位系统参数编号，三四位接入方编号，微信01，五六位平台编号，pc端01，app02，七八位对应服务的编号，05010101：网站鸿之微云，05010201：移动端hbox")
                                           @PathVariable("type") String type){
        String userId = SecurityUtils.getUserId();
        AssertUtil.assertThrow("未登陆，请先登陆", StrUtil.isBlank(userId));
        List<UserWeTripartiteBO> byTypeAndUserId = userWeChatService.getByTypeAndUserId(type, userId);
        if(CollectionUtil.isNotEmpty(byTypeAndUserId) && byTypeAndUserId.size()>0){
            return ResponseEntity.ok(Boolean.TRUE);
        }
        return ResponseEntity.ok(Boolean.FALSE);
    }

}
